package com.nativetalk.service.live;

import com.agora.DynamicKey4Sample;
import com.nativetalk.base.Params;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.FoundChange.TdNtMembFoundChangeInset;
import com.nativetalk.bean.FoundChange.TdNtTeaFoundChangeInsert;
import com.nativetalk.bean.*;
import com.nativetalk.bean.course.TdNtCourseLevelPageList;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.course.TwoTsNtCourseInfoDetail;
import com.nativetalk.bean.giving.TsNtLiveGivingList;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.TdNtMemberDetailOrders;
import com.nativetalk.bean.member.TdNtMemberStudyTimes;
import com.nativetalk.bean.member.course.TdNtMembCourseDetail;
import com.nativetalk.bean.member.setmeal.TdNtMembSetmealDetail;
import com.nativetalk.bean.purchase.TdNtMembPurchaseChange;
import com.nativetalk.bean.study.TdNtStudyTeachInsert;
import com.nativetalk.bean.study.TdNtStudyTeachUpdate;
import com.nativetalk.bean.teacher.*;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.Constant;
import com.nativetalk.common.getui.AndroidStudentGetui;
import com.nativetalk.common.getui.AndroidTeacherGetui;
import com.nativetalk.common.getui.IosStudentGetui;
import com.nativetalk.common.getui.IosTeacherGetui;
import com.nativetalk.common.util.*;
import com.nativetalk.dao.TdNtLiveDao;
import io.netty.channel.Channel;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

/**
 * Created by lizhun on 16/1/9.
 * Modified by wangzheng on 17/02/24
 */
@Service
public class LiveServiceImpl implements LiveService {
    private static Logger logger = Logger.getLogger(LiveServiceImpl.class);
    @Autowired
    private TdNtLiveDao tdNtLiveDao;
    static Timestamp timestamp = new Timestamp(new Date().getTime());
    static int ts = (int) (timestamp.getTime() / 1000);
    static int r = 12313;
    static long uid = 0;
    static int expiredTs = 0;
    static String appID = "612ca682ce3245a6b2b83e6dd54de535";

    @Override
    public RetInfo light(Params params) {
        String logInfo = this.getClass().getName() + ":light:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
            Map<String, Object> map = params.getParams();
            TdNtLiveLightHistory tdNtLiveLightHistory = new TdNtLiveLightHistory();
            tdNtLiveLightHistory.setLive_his_id(Integer.valueOf(map.get("live_cou_his_id").toString()));
            tdNtLiveLightHistory.setMemb_id(tdNtMember.getMemb_id());
            tdNtLiveLightHistory.setTea_id(Integer.valueOf(map.get("tea_id").toString()));
            tdNtLiveDao.insertTdNtLiveLightHistory(tdNtLiveLightHistory);
            Object object = MemcachedUtils.get("live" + map.get("live_cou_his_id"));
            int a = 0;
            if (object != null) {
                a = Integer.valueOf(object.toString());
            }

            Map<String, String> result = new HashMap<>();
            result.put("type", "light");
            result.put("popularity", a + "");
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo give(Params params) {
        String logInfo = this.getClass().getName() + ":give:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtLiveGivingList> tsNtLiveGivingListMap = (Map<String, TsNtLiveGivingList>) MemcachedUtils.get(MemcachedKey.tsNtLiveGivingListsMap);
            TdNtMember member = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
            TdNtMember tdNtMember = tdNtLiveDao.findTdNtMember(member.getMemb_id());
            Map<String, Object> map = params.getParams();
            TsNtLiveGivingList tsNtLiveGivingList = tsNtLiveGivingListMap.get(map.get("giv_id").toString());
            if (tdNtMember.getMemb_balance().compareTo(tsNtLiveGivingList.getGiv_amount()) > -1) {
                TdNtLiveGivingHistory tdNtLiveGivingHistory = new TdNtLiveGivingHistory();
                tdNtLiveGivingHistory.setGiv_amount(tsNtLiveGivingList.getGiv_amount());
                tdNtLiveGivingHistory.setLive_his_id(Integer.valueOf(map.get("live_cou_his_id").toString()));
                tdNtLiveGivingHistory.setGiv_icon(tsNtLiveGivingList.getGiv_icon());
                tdNtLiveGivingHistory.setGiv_id(tsNtLiveGivingList.getGiv_id());
                tdNtLiveGivingHistory.setGiv_name(tsNtLiveGivingList.getGiv_name());
                tdNtLiveGivingHistory.setGiv_popularity(tsNtLiveGivingList.getGiv_popularity());
                tdNtLiveGivingHistory.setHis_month(CommonUtil.getMonth());
                tdNtLiveGivingHistory.setMemb_id(tdNtMember.getMemb_id());
                tdNtLiveGivingHistory.setTea_id(Integer.valueOf(map.get("tea_id").toString()));
                tdNtLiveGivingHistory.setGiv_us_name(tsNtLiveGivingList.getGiv_us_name());
                tdNtLiveGivingHistory.setGiv_teacher_amount(tsNtLiveGivingList.getGiv_teacher_amount());
                tdNtLiveDao.insertTdNtLiveGivingHistory(tdNtLiveGivingHistory);
                /**
                 * 添加资金变动记录 更新用户余额
                 */
                TdNtMembFoundChangeInset tdNtMembFoundChangeInset = new TdNtMembFoundChangeInset();
                tdNtMembFoundChangeInset.setMemb_id(tdNtMember.getMemb_id());
                tdNtMembFoundChangeInset.setMfchg_change_amount(tsNtLiveGivingList.getGiv_amount());
                tdNtMembFoundChangeInset.setMfchg_change_month(CommonUtil.getMonth());
                //tdNtMembFoundChangeInset.setMfchg_change_name("购买" + tsNtLiveGivingList.getGiv_name());
                tdNtMembFoundChangeInset.setMfchg_change_name("直播打赏");
                tdNtMembFoundChangeInset.setMfchg_change_type("2");
                tdNtMembFoundChangeInset.setState("2");
                tdNtMembFoundChangeInset.setType(4);
                tdNtMembFoundChangeInset.setMfchg_number(CommonUtil.getOrderNub());
                tdNtMembFoundChangeInset.setMfchg_back_amount(tdNtMember.getMemb_balance().subtract(tsNtLiveGivingList.getGiv_amount()));
                tdNtMembFoundChangeInset.setMfchg_front_amount(tdNtMember.getMemb_balance());
                tdNtLiveDao.insertTdNtMembFoundChange(tdNtMembFoundChangeInset);
                tdNtMember.setMemb_balance(tdNtMembFoundChangeInset.getMfchg_back_amount());
                tdNtLiveDao.updateTdNtMemberBalance(tdNtMember);
                /**
                 * 添加教师资金变动记录, 更新教师余额
                 */
                /**
                 * 添加教师资金变动记录
                 */
                TdNtTeacherIndex tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherIndex(tdNtLiveGivingHistory.getTea_id());
                TdNtTeaFoundChangeInsert tdNtTeaFoundChangeInsert = new TdNtTeaFoundChangeInsert();
                tdNtTeaFoundChangeInsert.setState("2");
                tdNtTeaFoundChangeInsert.setTea_id(tdNtLiveGivingHistory.getTea_id());
                tdNtTeaFoundChangeInsert.setTfchg_account(new BigDecimal("0"));
                tdNtTeaFoundChangeInsert.setTfchg_change_amount(tdNtLiveGivingHistory.getGiv_teacher_amount());
                tdNtTeaFoundChangeInsert.setTfchg_back_amount(tdNtTeacherIndex.getTea_balance().add(tdNtLiveGivingHistory.getGiv_teacher_amount()));
                tdNtTeaFoundChangeInsert.setTfchg_change_month(CommonUtil.getMonth());
                tdNtTeaFoundChangeInsert.setTfchg_change_type("1");
                tdNtTeaFoundChangeInsert.setTfchg_counter_fee(new BigDecimal("0"));
                tdNtTeaFoundChangeInsert.setTfchg_front_amount(tdNtTeacherIndex.getTea_balance());
                tdNtTeaFoundChangeInsert.setTfchg_number(CommonUtil.getOrderNub());
                tdNtTeaFoundChangeInsert.setTfchg_change_name(tdNtLiveGivingHistory.getGiv_us_name());
                tdNtTeaFoundChangeInsert.setType(3);
                tdNtLiveDao.insertTdNtTeaFoundChangeInsert(tdNtTeaFoundChangeInsert);
                /**
                 * 修改教师余额
                 */
                tdNtTeacherIndex.setTea_balance(tdNtTeaFoundChangeInsert.getTfchg_back_amount());
                tdNtTeacherIndex.setTea_id(tdNtLiveGivingHistory.getTea_id());
                tdNtTeacherIndex.setTea_total_balance(tdNtTeacherIndex.getTea_total_balance().add(tdNtTeaFoundChangeInsert.getTfchg_change_amount()));
                tdNtLiveDao.updateTdNtTeacherIndex(tdNtTeacherIndex);
                Object object = MemcachedUtils.get("live" + map.get("live_cou_his_id"));
                int a = tdNtLiveGivingHistory.getGiv_popularity();
                if (object != null) {
                    a = Integer.valueOf(object.toString()) + a;
                    MemcachedUtils.replace("live" + map.get("live_cou_his_id"), a, 2 * 60 * 60 * 1000);
                } else {

                    MemcachedUtils.add("live" + map.get("live_cou_his_id"), a, 2 * 60 * 60 * 1000);
                }
                Map<String, String> result = new HashMap<>();
                result.put("memb_name", tdNtMember.getMemb_name());
                result.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                result.put("giv_id", tsNtLiveGivingList.getGiv_id() + "");
                result.put("type", "give");
                result.put("popularity", a + "");
                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setObj(result);
            } else {
                retInfo.setMark("1");
                retInfo.setTip("余额不足");
            }
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo randomKallTeacer(Params params, Channel channel) throws Exception {
        String logInfo = this.getClass().getName() + ":randomKallTeacer:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        String cou_id = params.getParams().get("cou_id").toString();
        String conference_title = params.getParams().get("conference_title").toString();
        String conference_url = params.getParams().get("conference_url").toString();
        String conference_password = params.getParams().get("conference_password").toString();

        TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
        Map<String, TwoTsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TwoTsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMapTwo);
        TwoTsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
        TdNtMembSetmealDetail tdNtMembSetmealDetail = new TdNtMembSetmealDetail();
        TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
        try {
            //判断课程是否可用
            if (tsNtCourseInfoDetail != null) {
                //获取套餐的剩余时间
                Map<String, Object> map1 = new HashMap<>();
                map1.put("memb_id", tdNtMember.getMemb_id());
                map1.put("cou_id", tsNtCourseInfoDetail.getCate_id());
                tdNtMembSetmealDetail = tdNtLiveDao.findTdNtMembSetmealDetail(map1);
                if (tdNtMembSetmealDetail == null || tdNtMembSetmealDetail.getSurplus_time() <= 0) {

                    retInfo.setMark("1");
                    retInfo.setTip("课程余额不足");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", "randomKallTeacer");
                    retInfo.setObj(result);
                    return retInfo;
                }
                tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                tdNtStudyTeachInsert.setStudy_cou_picture(tsNtCourseInfoDetail.getStand());
                tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                tdNtStudyTeachInsert.setStudy_state("0");
                tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachInsert.setLaunch_time(timestamp);
                tdNtStudyTeachInsert.setConference_title(conference_title);
                tdNtStudyTeachInsert.setConference_url(conference_url);
                tdNtStudyTeachInsert.setConference_password(conference_password);
                tdNtLiveDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);

            } else {
                retInfo.setMark("1");
                retInfo.setTip("课程不存在");
                Map<String, Object> result = new HashMap<>();
                result.put("type", "randomKallTeacer");
                retInfo.setObj(result);
                return retInfo;
            }


        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            Map<String, Object> result = new HashMap<>();
            result.put("type", "randomKallTeacer");
            retInfo.setObj(result);
            logger.error(e.getMessage(), e);
        } finally {
            TdNtMembCourseDetail tdNtMembCourse = new TdNtMembCourseDetail();
            tdNtMembCourse.setCou_id(Integer.valueOf(cou_id));
            tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
            tdNtMembCourse = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourse);

            FindTeacher findTeacher = new FindTeacher();
            findTeacher.run(tdNtLiveDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id()
                    , tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(), tsNtCourseInfoDetail.getCou_english_name(), channel);
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtLiveDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
            if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                Map<String, Object> map = new HashMap<>();
                if (tdNtMembCourse == null) {
                    map.put("studySize", 1);
                } else {
                    map.put("studySize", tdNtMembCourse.getStudy_page());
                }
                map.put("surplus_time", tdNtMembSetmealDetail.getSurplus_time());
                Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
                double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
                map.put("sutdy_amount", sutdy_amount);
                TdNtMember member = tdNtLiveDao.findTdNtMember(tdNtMember.getMemb_id());
                map.put("memb_name", member.getMemb_name());
                map.put("memb_head_portrait", member.getMemb_head_portrait());
                map.put("video_switch", member.getVideo_switch());


                map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                map.put("tea_name", tdNtTeacherIndex.getTea_name());
                map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);

                map.put("course_pictures", tdNtCoursePictureLists);

                /**
                 * 每个课程的每一章节开始对应的页数列表
                 */
                Map<String, List<TdNtCourseLevelPageList>> tdNtCourseLevelPageListMap = (Map<String, List<TdNtCourseLevelPageList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelPageListMap);
                List<TdNtCourseLevelPageList> tdNtCourseLevelPageLists = tdNtCourseLevelPageListMap.get(cou_id);
                //该课程没有配章节
                if (null == tdNtCourseLevelPageLists) {
                    tdNtCourseLevelPageLists = new ArrayList<>();
                    TdNtCourseLevelPageList tdNtCourseLevelPageList = new TdNtCourseLevelPageList();
                    tdNtCourseLevelPageList.setCou_id(100);
                    tdNtCourseLevelPageList.setStart_page(1);
                    tdNtCourseLevelPageList.setEnd_page(tdNtCoursePictureLists.size());
                    tdNtCourseLevelPageList.setLevel_sort(1);
                    tdNtCourseLevelPageList.setLevel_english_name("");
                    tdNtCourseLevelPageLists.add(tdNtCourseLevelPageList);
                }
                map.put("course_level_page", tdNtCourseLevelPageLists);

                String study_channel = tdNtTeacherIndex.getToken() + tdNtStudyTeachInsert.getStt_id();
                tdNtStudyTeachInsert.setChannel(study_channel);
                tdNtLiveDao.updateTdNtStudyTeachChannel(tdNtStudyTeachInsert);
                map.put("channel", study_channel);

                map.put("recordKey", DynamicKey4Sample.getRecordKey(appID, study_channel, ts, r, uid, expiredTs));
                map.put("channelKey", DynamicKey4Sample.getChannelKey(appID, study_channel, ts, r, uid, expiredTs));
                map.put("ts", ts);
                map.put("r", r);
                map.put("uid", uid);
                map.put("appID", appID);
                map.put("expiredTs", expiredTs);
                map.put("type", "randomKallTeacer");

                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setObj(map);

                /**
                 * 接通发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>) MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0; i < tdNtTeacherGetuisCached.size(); i++) {
                        if (!tdNtTeacherGetuisCached.get(i).getTea_id().equals(tdNtStudyTeachDetail.getTea_id())) {
                            if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                                AndroidTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());
                            } else {
                                IosTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());

                            }
                        }


                    }
                }
            } else if (tdNtStudyTeachDetail.getStudy_state().equals("6")) {
                Map<String, Object> result = new HashMap<>();
                result.put("type", params.getFunctionName());
                retInfo.setObj(result);
                retInfo.setMark("1");
                retInfo.setTip("老师拒绝");
                retInfo.setObj(result);
            } else {
                tdNtStudyTeachDetail.setStudy_state("6");
                tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtLiveDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                retInfo.setMark("1");
                retInfo.setTip("取消成功");
                Map<String, Object> result = new HashMap<>();
                result.put("type", "randomKallTeacer");
                retInfo.setObj(result);
                /**
                 * 取消发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>) MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0; i < tdNtTeacherGetuisCached.size(); i++) {
                        if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                            AndroidTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());
                        } else {
                            IosTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());

                        }

                    }
                }
            }

        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo kallTeacer(Params params, Channel channel) throws Exception {
        String logInfo = this.getClass().getName() + ":kallTeacer:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        String cou_id = params.getParams().get("cou_id").toString();
        String tea_id = params.getParams().get("tea_id").toString();
        String conference_title = params.getParams().get("conference_title").toString();
        String conference_url = params.getParams().get("conference_url").toString();
        String conference_password = params.getParams().get("conference_password").toString();

        TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
        TdNtTeacherGetui tdNtTeacherGetui = tdNtLiveDao.findTdNtTeacherByTeaId(Integer.valueOf(tea_id));
        /**
         * 判断老师是否存在及是否在线
         */
        if (tdNtTeacherGetui != null && MemcachedUtils.get(tdNtTeacherGetui.getToken()) != null) {
            TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
            Map<String, TwoTsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TwoTsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMapTwo);
            TwoTsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
            TdNtMembSetmealDetail tdNtMembSetmealDetail = new TdNtMembSetmealDetail();
            try {
                //判断课程是否可用
                if (tsNtCourseInfoDetail != null) {
                    //获取套餐的剩余时间
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("memb_id", tdNtMember.getMemb_id());
                    map1.put("cou_id", tsNtCourseInfoDetail.getCate_id());
                    tdNtMembSetmealDetail = tdNtLiveDao.findTdNtMembSetmealDetail(map1);
                    if (tdNtMembSetmealDetail == null || tdNtMembSetmealDetail.getSurplus_time() <= 0) {

                        retInfo.setMark("1");
                        retInfo.setTip("课程余额不足");
                        Map<String, Object> result = new HashMap<>();
                        result.put("type", "kallTeacer");
                        retInfo.setObj(result);
                        return retInfo;
                    }
                    tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                    tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                    tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                    tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                    tdNtStudyTeachInsert.setStudy_cou_picture(tsNtCourseInfoDetail.getStand());
                    tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                    tdNtStudyTeachInsert.setStudy_state("0");
                    tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachInsert.setLaunch_time(timestamp);
                    tdNtStudyTeachInsert.setConference_title(conference_title);
                    tdNtStudyTeachInsert.setConference_url(conference_url);
                    tdNtStudyTeachInsert.setConference_password(conference_password);
                    tdNtLiveDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);

                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("课程不存在");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", "kallTeacer");
                    retInfo.setObj(result);
                    return retInfo;
                }


            } catch (Exception e) {
                retInfo.setMark("-1");
                retInfo.setTip("json数据错误");
                Map<String, Object> result = new HashMap<>();
                result.put("type", "kallTeacer");
                retInfo.setObj(result);
                logger.error(e.getMessage(), e);
            } finally {
                TdNtMembCourseDetail tdNtMembCourse = new TdNtMembCourseDetail();
                tdNtMembCourse.setCou_id(Integer.valueOf(cou_id));
                tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
                tdNtMembCourse = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourse);
                CallTeacher callTeacher = new CallTeacher();
                callTeacher.run(tdNtLiveDao, tdNtStudyTeachInsert.getStt_id(),
                        tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(), tsNtCourseInfoDetail.getCou_english_name(), tdNtTeacherGetui, channel);
                TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtLiveDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
                if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                    TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                    Map<String, Object> map = new HashMap<>();
                    if (tdNtMembCourse == null) {
                        map.put("studySize", 1);
                    } else {
                        map.put("studySize", tdNtMembCourse.getStudy_page());
                    }
                    map.put("surplus_time", tdNtMembSetmealDetail.getSurplus_time());
                    TdNtMember member = tdNtLiveDao.findTdNtMember(tdNtMember.getMemb_id());
                    map.put("memb_name", member.getMemb_name());
                    map.put("memb_head_portrait", member.getMemb_head_portrait());
                    map.put("video_switch", member.getVideo_switch());

                    map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                    map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                    map.put("tea_name", tdNtTeacherIndex.getTea_name());
                    map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                    Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);
                    map.put("course_pictures", tdNtCoursePictureLists);

                    /**
                     * 每个课程的每一章节开始对应的页数列表
                     */
                    Map<String, List<TdNtCourseLevelPageList>> tdNtCourseLevelPageListMap = (Map<String, List<TdNtCourseLevelPageList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelPageListMap);
                    List<TdNtCourseLevelPageList> tdNtCourseLevelPageLists = tdNtCourseLevelPageListMap.get(cou_id);
                    //该课程没有配章节
                    if (null == tdNtCourseLevelPageLists) {
                        tdNtCourseLevelPageLists = new ArrayList<>();
                        TdNtCourseLevelPageList tdNtCourseLevelPageList = new TdNtCourseLevelPageList();
                        tdNtCourseLevelPageList.setCou_id(100);
                        tdNtCourseLevelPageList.setStart_page(1);
                        tdNtCourseLevelPageList.setEnd_page(tdNtCoursePictureLists.size());
                        tdNtCourseLevelPageList.setLevel_sort(1);
                        tdNtCourseLevelPageList.setLevel_english_name("");
                        tdNtCourseLevelPageLists.add(tdNtCourseLevelPageList);
                    }
                    map.put("course_level_page", tdNtCourseLevelPageLists);

                    String study_channel = tdNtTeacherIndex.getToken() + tdNtStudyTeachInsert.getStt_id();
                    tdNtStudyTeachInsert.setChannel(study_channel);
                    tdNtLiveDao.updateTdNtStudyTeachChannel(tdNtStudyTeachInsert);
                    map.put("channel", study_channel);
                    ts = ts + 2 * 60 * 60;
                    map.put("recordKey", DynamicKey4Sample.getRecordKey(appID, study_channel, ts, r, uid, expiredTs));
                    map.put("channelKey", DynamicKey4Sample.getChannelKey(appID, study_channel, ts, r, uid, expiredTs));
                    map.put("ts", ts);
                    map.put("r", r);
                    map.put("uid", uid);
                    map.put("appID", appID);
                    map.put("expiredTs", expiredTs);
                    map.put("type", "kallTeacer");

                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                    retInfo.setObj(map);
                } else if (tdNtStudyTeachDetail.getStudy_state().equals("7")) {
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    retInfo.setMark("1");
                    retInfo.setTip("老师拒绝");
                    retInfo.setObj(result);
                } else {
                    tdNtStudyTeachDetail.setStudy_state("6");
                    tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                    tdNtLiveDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                    retInfo.setMark("1");
                    retInfo.setTip("取消成功");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", "kallTeacer");
                    retInfo.setObj(result);
                    /**
                     * 取消发送推送
                     */
                    if (tdNtTeacherGetui.getEqu_type().equals("1")) {
                        AndroidTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());
                    } else {
                        IosTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());

                    }
                }

            }
        } else {
            retInfo.setMark("1");
            retInfo.setTip("对不起,您呼叫的老师不在线!");
            Map<String, Object> result = new HashMap<>();
            result.put("type", "kallTeacer");
            retInfo.setObj(result);
            /**
             * 将老师修改为不在线
             */
            TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
            tdNtTeacherState.setTea_id(Integer.valueOf(tea_id));
            tdNtTeacherState.setState(Constant.Teacher.State.offline);
            tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.offline);
            tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo kallTeacerFreeCourse(Params params, Channel channel) throws Exception {
        String logInfo = this.getClass().getName() + ":kallTeacer:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        String tea_id = params.getParams().get("tea_id").toString();
        String conference_title = params.getParams().get("conference_title").toString();
        String conference_url = params.getParams().get("conference_url").toString();
        String conference_password = params.getParams().get("conference_password").toString();

        TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
        TdNtTeacherGetui tdNtTeacherGetui = tdNtLiveDao.findTdNtTeacherByTeaId(Integer.valueOf(tea_id));
        TdNtMember member = tdNtLiveDao.findTdNtMember(tdNtMember.getMemb_id());
        /**
         * 判断老师是否存在及是否在线
         */
        String study_channel = "";
        if (tdNtTeacherGetui != null && MemcachedUtils.get(tdNtTeacherGetui.getToken()) != null) {
            TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
            TwoTsNtCourseInfoDetail tsNtCourseInfoDetail = (TwoTsNtCourseInfoDetail) MemcachedUtils.get(MemcachedKey.freeCourse);
            try {
                //判断课程是否可用
                if (tsNtCourseInfoDetail != null) {

                    //从缓存中读取老师每分钟的教学费用
                    Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
                    //自由话题学生按分钟收费， 每分钟费用在数据库ts_nt_website_conf表中配置
                    BigDecimal stu_amount = new BigDecimal(tsNtWebSiteConfIndexMap.get("25").getData_value());
                    if (member.getMemb_balance().compareTo(stu_amount) < 0) {

                        retInfo.setMark("1");
                        retInfo.setTip("余额不足");
                        Map<String, Object> result = new HashMap<>();
                        result.put("type", params.getFunctionName());
                        retInfo.setObj(result);
                        return retInfo;
                    }

                    tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                    tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                    tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                    tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                    tdNtStudyTeachInsert.setStudy_cou_picture(tsNtCourseInfoDetail.getStand());
                    tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                    tdNtStudyTeachInsert.setStudy_state("0");
                    tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachInsert.setLaunch_time(timestamp);
                    tdNtStudyTeachInsert.setConference_title(conference_title);
                    tdNtStudyTeachInsert.setConference_url(conference_url);
                    tdNtStudyTeachInsert.setConference_password(conference_password);
                    tdNtLiveDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                    study_channel = tdNtTeacherGetui.getToken() + tdNtStudyTeachInsert.getStt_id();
                    tdNtStudyTeachInsert.setChannel(study_channel);
                    tdNtLiveDao.updateTdNtStudyTeachChannel(tdNtStudyTeachInsert);
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("课程不存在");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    return retInfo;
                }


            } catch (Exception e) {
                retInfo.setMark("-1");
                retInfo.setTip("json数据错误");
                Map<String, Object> result = new HashMap<>();
                result.put("type", params.getFunctionName());
                retInfo.setObj(result);
                logger.error(e.getMessage(), e);
            } finally {

                CallTeacher callTeacher = new CallTeacher();

                //
                callTeacher.run(tdNtLiveDao, tdNtStudyTeachInsert.getStt_id(),
                        tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(), tsNtCourseInfoDetail.getCou_english_name(), tdNtTeacherGetui, channel);
                TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtLiveDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
                if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                    TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                    Map<String, Object> map = new HashMap<>();
                    map.put("memb_balance", member.getMemb_balance());
                    map.put("memb_name", member.getMemb_name());
                    map.put("memb_head_portrait", member.getMemb_head_portrait());
                    map.put("video_switch", member.getVideo_switch());

                    //获取当前课程学习页数
                    TdNtMembCourseDetail tdNtMembCourse = new TdNtMembCourseDetail();
                    tdNtMembCourse.setCou_id(tsNtCourseInfoDetail.getCou_id());
                    tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
                    tdNtMembCourse = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourse);
                    if (tdNtMembCourse == null) {
                        map.put("studySize", 1);
                    } else {
                        map.put("studySize", tdNtMembCourse.getStudy_page());
                    }

                    map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                    map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                    map.put("tea_name", tdNtTeacherIndex.getTea_name());
                    map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                    Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(Constant.freetalk_cou_id);
                    map.put("course_pictures", tdNtCoursePictureLists);

                    Map<String, List<TdNtCourseLevelPageList>> tdNtCourseLevelPageListMap = (Map<String, List<TdNtCourseLevelPageList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelPageListMap);
                    List<TdNtCourseLevelPageList> tdNtCourseLevelPageLists = tdNtCourseLevelPageListMap.get(Constant.freetalk_cou_id);
                    //该课程没有配章节
                    if (null == tdNtCourseLevelPageLists) {
                        tdNtCourseLevelPageLists = new ArrayList<>();
                        TdNtCourseLevelPageList tdNtCourseLevelPageList = new TdNtCourseLevelPageList();
                        tdNtCourseLevelPageList.setCou_id(100);
                        tdNtCourseLevelPageList.setStart_page(1);
                        tdNtCourseLevelPageList.setEnd_page(tdNtCoursePictureLists.size());
                        tdNtCourseLevelPageList.setLevel_sort(1);
                        tdNtCourseLevelPageList.setLevel_english_name("freetalk");
                        tdNtCourseLevelPageLists.add(tdNtCourseLevelPageList);
                    }
                    map.put("course_level_page", tdNtCourseLevelPageLists);

                    map.put("channel", study_channel);

                    map.put("recordKey", DynamicKey4Sample.getRecordKey(appID, study_channel, ts, r, uid, expiredTs));
                    map.put("channelKey", DynamicKey4Sample.getChannelKey(appID, study_channel, ts, r, uid, expiredTs));
                    map.put("ts", ts);
                    map.put("r", r);
                    map.put("uid", uid);
                    map.put("appID", appID);
                    map.put("expiredTs", expiredTs);
                    map.put("type", params.getFunctionName());

                    System.out.println("kallTeacerFreeCourse:ts:" + ts);
                    System.out.println("kallTeacerFreeCourse:channel:" + study_channel);
                    System.out.println("kallTeacerFreeCourse:channelKey:" + DynamicKey4Sample.getChannelKey(appID, study_channel, ts, r, uid, expiredTs));

                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                    retInfo.setObj(map);
                } else if (tdNtStudyTeachDetail.getStudy_state().equals("7")) {
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    retInfo.setMark("1");
                    retInfo.setTip("老师拒绝");
                    retInfo.setObj(result);
                } else {
                    tdNtStudyTeachDetail.setStudy_state("6");
                    tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                    tdNtLiveDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                    retInfo.setMark("1");
                    retInfo.setTip("取消成功");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    /**
                     * 取消发送推送
                     */
                    if (tdNtTeacherGetui.getEqu_type().equals("1")) {
                        AndroidTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());
                    } else {
                        IosTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());

                    }
                    if (MemcachedUtils.get("order" + tdNtStudyTeachDetail.getTea_id()) != null) {
                        /**
                         * 两次不接单将老师下线
                         */
                        int size = Integer.valueOf(MemcachedUtils.get("order" + tdNtTeacherGetui.getTea_id()).toString());
                        if (size >= 2) {
                            TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
                            tdNtTeacherState.setTea_id(Integer.valueOf(tea_id));
                            tdNtTeacherState.setState(Constant.Teacher.State.offline);
                            tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.offline);
                            tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
                        }
                    }
                }

            }
        } else {

            retInfo.setMark("1");
            retInfo.setTip("对不起,您呼叫的老师不在线!");
            Map<String, Object> result = new HashMap<>();
            result.put("type", params.getFunctionName());
            retInfo.setObj(result);
            /**
             * 将老师修改为不在线
             */
            TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
            tdNtTeacherState.setTea_id(Integer.valueOf(tea_id));
            tdNtTeacherState.setState(Constant.Teacher.State.offline);
            tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.offline);
            tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo eceivingOrders(Params params) {
        String logInfo = this.getClass().getName() + ":eceivingOrders:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            uid = 0;
            int stt_id = Integer.valueOf(params.getParams().get("stt_id").toString());
            TdNtStudyTeachUpdate tdNtStudyTeachUpdate = tdNtLiveDao.findTdNtStudyTeachUpdate(stt_id);
            if (tdNtStudyTeachUpdate != null && tdNtStudyTeachUpdate.getStudy_state().equals("0")) {
                if (params.getFunctionName().equals("cancelOrders")) {
                    tdNtStudyTeachUpdate.setStudy_state("6");
                    tdNtStudyTeachUpdate.setEnd_time(new Timestamp(System.currentTimeMillis()));
                    tdNtLiveDao.updateTdNtStudyTeachCancel(tdNtStudyTeachUpdate);
                    retInfo.setMark("0");
                    retInfo.setTip("取消成功");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", "cancelOrders");
                    retInfo.setObj(result);

                } else {
                    TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(params.getMustParams().get("token"));
                    tdNtStudyTeachUpdate.setStudy_state("1");
                    tdNtStudyTeachUpdate.setTea_id(tdNtTeacherResult.getTea_id());
                    tdNtStudyTeachUpdate.setStart_time(new Timestamp(System.currentTimeMillis()));

                    tdNtLiveDao.updateTdNtStudyTeach(tdNtStudyTeachUpdate);

                    TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
                    tdNtTeacherState.setTea_id(tdNtStudyTeachUpdate.getTea_id());
                    tdNtTeacherState.setState(Constant.Teacher.State.offline);
                    tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.offline);
                    TdNtMemberDetailOrders tdNtMemberDetailOrders = tdNtLiveDao.findTdNtMemberDetailOrders(tdNtStudyTeachUpdate.getMemb_id());
                    tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
                    retInfo.setMark("0");
                    retInfo.setTip("接单成功");
                    Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get("" + tdNtStudyTeachUpdate.getCou_id());
                    retInfo.setObj(tdNtCoursePictureLists);

                    //获取当前课程学习页数
                    TdNtMembCourseDetail tdNtMembCourse = new TdNtMembCourseDetail();
                    tdNtMembCourse.setCou_id(tdNtStudyTeachUpdate.getCou_id());
                    tdNtMembCourse.setMemb_id(tdNtStudyTeachUpdate.getMemb_id());
                    tdNtMembCourse = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourse);
                    Map<String, Object> map = new HashMap<>();
                    if (Constant.freetalk_cou_id.equals(tdNtStudyTeachUpdate.getCou_id().toString())) {
                        map.put("video_switch", 0);
                    } else {
                        map.put("video_switch", tdNtMemberDetailOrders.getVideo_switch());
                    }
                    if (tdNtMembCourse == null) {
                        map.put("studySize", 1);
                    } else {
                        map.put("studySize", tdNtMembCourse.getStudy_page());
                    }
                    map.put("course_pictures", tdNtCoursePictureLists);
                    /**
                     * 每个课程的每一章节开始对应的页数列表
                     */
                    Map<String, List<TdNtCourseLevelPageList>> tdNtCourseLevelPageListMap = (Map<String, List<TdNtCourseLevelPageList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelPageListMap);
                    List<TdNtCourseLevelPageList> tdNtCourseLevelPageLists = tdNtCourseLevelPageListMap.get("" + tdNtStudyTeachUpdate.getCou_id());
                    //该课程没有配章节
                    if (null == tdNtCourseLevelPageLists || 0 == tdNtCourseLevelPageLists.size()) {
                        tdNtCourseLevelPageLists = new ArrayList<>();
                        TdNtCourseLevelPageList tdNtCourseLevelPageList = new TdNtCourseLevelPageList();
                        tdNtCourseLevelPageList.setCou_id(100);
                        tdNtCourseLevelPageList.setStart_page(1);
                        tdNtCourseLevelPageList.setEnd_page(tdNtCoursePictureLists.size());
                        tdNtCourseLevelPageList.setLevel_sort(1);
                        if (100 == tdNtStudyTeachUpdate.getCou_id()) {
                            tdNtCourseLevelPageList.setLevel_english_name("freetalk");
                        } else {
                            tdNtCourseLevelPageList.setLevel_english_name("");
                        }
                        tdNtCourseLevelPageLists.add(tdNtCourseLevelPageList);
                    }
                    map.put("course_level_page", tdNtCourseLevelPageLists);

                    map.put("student_channel", tdNtMemberDetailOrders.getToken());
                    map.put("type", params.getFunctionName());
                    int ts = (int) (tdNtStudyTeachUpdate.getLaunch_time().getTime() / 1000);
                    map.put("channel", tdNtStudyTeachUpdate.getChannel());
                    map.put("recordKey", DynamicKey4Sample.getRecordKey(appID, tdNtStudyTeachUpdate.getChannel(), ts, r, uid, expiredTs));
                    map.put("channelKey", DynamicKey4Sample.getChannelKey(appID, tdNtStudyTeachUpdate.getChannel(), ts, r, uid, expiredTs));
                    map.put("ts", ts);
                    map.put("r", r);
                    map.put("uid", uid);
                    map.put("appID", appID);
                    map.put("expiredTs", expiredTs);
                    map.put("conference_title", tdNtStudyTeachUpdate.getConference_title());
                    map.put("conference_url", tdNtStudyTeachUpdate.getConference_url());
                    map.put("conference_password", tdNtStudyTeachUpdate.getConference_password());
                    retInfo.setObj(map);
                    System.out.println(tdNtStudyTeachUpdate.getLaunch_time());
                    System.out.println("eceivingOrders:ts:" + ts);
                    System.out.println("eceivingOrders:channel:" + tdNtStudyTeachUpdate.getChannel());
                    System.out.println("eceivingOrders:channelKey:" + DynamicKey4Sample.getChannelKey(appID, tdNtStudyTeachUpdate.getChannel(), ts, r, uid, expiredTs));
                    if (MemcachedUtils.get("order" + tdNtStudyTeachUpdate.getTea_id()) != null) {
                        MemcachedUtils.delete("order" + tdNtStudyTeachUpdate.getTea_id());
                    }

                }

            } else {
                if (params.getFunctionName().equals("cancelOrders")) {
                    retInfo.setMark("1");
                    retInfo.setTip("已经接单");
                    TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
                    Map<String, Object> result = new HashMap<>();
                    result.put("memb_name", tdNtMember.getMemb_name());
                    result.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                    result.put("stt_id", stt_id);
                    TdNtTeacherIndex tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherIndex(tdNtStudyTeachUpdate.getTea_id());
                    result.put("tea_name", tdNtTeacherIndex.getTea_name());
                    result.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = new ArrayList<>();
                    if (Constant.freetalk_cou_id.equals(tdNtStudyTeachUpdate.getCou_id().toString())) {

                        tdNtCoursePictureLists.add(0, (TdNtCoursePictureList) MemcachedUtils.get(MemcachedKey.freeCoursePicture));
                    } else {

                        Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                        tdNtCoursePictureLists = tdNtCoursePictureListMap.get("" + tdNtStudyTeachUpdate.getCou_id());
                    }
                    int ts = (int) tdNtStudyTeachUpdate.getLaunch_time().getTime() / 1000;
                    result.put("course_pictures", tdNtCoursePictureLists);

                    /**
                     * 每个课程的每一章节开始对应的页数列表
                     */
                    Map<String, List<TdNtCourseLevelPageList>> tdNtCourseLevelPageListMap = (Map<String, List<TdNtCourseLevelPageList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelPageListMap);
                    List<TdNtCourseLevelPageList> tdNtCourseLevelPageLists = tdNtCourseLevelPageListMap.get("" + tdNtStudyTeachUpdate.getCou_id());
                    //该课程没有配章节
                    if (null == tdNtCourseLevelPageLists || 0 == tdNtCourseLevelPageLists.size()) {
                        tdNtCourseLevelPageLists = new ArrayList<>();
                        TdNtCourseLevelPageList tdNtCourseLevelPageList = new TdNtCourseLevelPageList();
                        tdNtCourseLevelPageList.setCou_id(tdNtStudyTeachUpdate.getCou_id());
                        tdNtCourseLevelPageList.setStart_page(1);
                        tdNtCourseLevelPageList.setEnd_page(tdNtCoursePictureLists.size());
                        tdNtCourseLevelPageList.setLevel_sort(1);
                        if (Integer.valueOf(Constant.freetalk_cou_id) == tdNtStudyTeachUpdate.getCou_id()) {
                            tdNtCourseLevelPageList.setLevel_english_name("freetalk");
                        } else {
                            tdNtCourseLevelPageList.setLevel_english_name("");
                        }
                        tdNtCourseLevelPageLists.add(tdNtCourseLevelPageList);
                    }
                    result.put("course_level_page", tdNtCourseLevelPageLists);

                    result.put("channel", tdNtStudyTeachUpdate.getChannel());
                    result.put("recordKey", DynamicKey4Sample.getRecordKey(appID, tdNtStudyTeachUpdate.getChannel(), ts, r, uid, expiredTs));
                    result.put("channelKey", DynamicKey4Sample.getChannelKey(appID, tdNtStudyTeachUpdate.getChannel(), ts, r, uid, expiredTs));
                    result.put("ts", ts);
                    result.put("r", r);
                    result.put("uid", uid);
                    result.put("appID", appID);
                    result.put("expiredTs", expiredTs);
                    retInfo.setObj(result);
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("Aw shucks! This call was taken by another teacher. Please wait for another student to call");
                }
                Map<String, Object> result = new HashMap<>();
                result.put("type", params.getFunctionName());
                retInfo.setObj(result);
            }
            /**
             * 将老师修改为忙碌
             */
            TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
            tdNtTeacherState.setTea_id(tdNtStudyTeachUpdate.getTea_id());
            tdNtTeacherState.setState(Constant.Teacher.State.busy);
            tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.busy);
            tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("missed call");
            Map<String, Object> result = new HashMap<>();
            result.put("type", params.getFunctionName());
            retInfo.setObj(result);
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override

    @Transactional
    public synchronized RetInfo endOrders(Params params) {
        String logInfo = this.getClass().getName() + ":end:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int stt_id = Integer.valueOf(params.getParams().get("stt_id").toString());
            TdNtStudyTeachUpdate tdNtStudyTeachUpdate = tdNtLiveDao.findTdNtStudyTeachUpdate(stt_id);

            //从缓存中读取老师每分钟的教学费用
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            //教学费用 老师每分钟0.2美分
            BigDecimal tea_amount = new BigDecimal(tsNtWebSiteConfIndexMap.get("24").getData_value());//0.2

            if (tdNtStudyTeachUpdate != null && tdNtStudyTeachUpdate.getStudy_state().equals("1")) {
                TdNtTeacherIndex tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherIndex(tdNtStudyTeachUpdate.getTea_id());
                /**
                 * 计算教学价格  修改教学记录信息
                 */
                Long now = System.currentTimeMillis();
                Long teach_time = (now - tdNtStudyTeachUpdate.getStart_time().getTime()) / 1000;//实际上课时长，单位秒
                int minute = new Integer((int) (teach_time / 60));//实际上课时长，单位分钟

                int tea_minute = minute;//老师教学实际时间（分钟），不足一分钟舍掉
                if ((teach_time % 60) != 0) {
                    minute = minute + 1;
                }
                if ((now - tdNtStudyTeachUpdate.getStart_time().getTime()) <= 10000) {
                    minute = 0;
                }

                /**
                 * 查询用户余额及用户套餐
                 */
                Map<String, TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);

                TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(tdNtStudyTeachUpdate.getCou_id() + "");

                Map<String, Object> map = new HashMap<>();
                map.put("memb_id", tdNtStudyTeachUpdate.getMemb_id());
                map.put("cou_id", tsNtCourseInfoDetail.getCate_id());
                tdNtStudyTeachUpdate.setEnd_time(new Timestamp(now));
                tdNtStudyTeachUpdate.setStudy_time(minute);
                tdNtStudyTeachUpdate.setStudy_state("3");//3待评价
                tdNtStudyTeachUpdate.setTea_study_time(tea_minute);
                tdNtStudyTeachUpdate.setTea_study_amount(tea_amount.multiply(new BigDecimal(tea_minute)));
                TdNtTeaFoundChangeInsert tdNtTeaFoundChangeInsert = new TdNtTeaFoundChangeInsert();
                TdNtReserveHistory tdNtReserveHistory = tdNtLiveDao.findTdNtReserveHistoryBySttId(stt_id);
/*
                if (tdNtReserveHistory != null){
                    tdNtStudyTeachUpdate.setStudy_amount(new BigDecimal(0));
                    tdNtReserveHistory.setReserve_his_end_time(new Timestamp(System.currentTimeMillis()));
                    long time = tdNtReserveHistory.getReserve_his_end_time().getTime() - tdNtReserveHistory.getReserve_his_start_time().getTime();
                    tdNtReserveHistory.setReserve_his_time((int)time/60/1000);
                    tdNtLiveDao.updateTdNtReserveHistory(tdNtReserveHistory);
                }
                else {*/
                //自由话题
                if (Constant.freetalk_cou_id.equals(tdNtStudyTeachUpdate.getCou_id().toString())) {
                    //自由话题学生按分钟收费， 每分钟费用在数据库ts_nt_website_conf表中配置
                    BigDecimal stu_amount = new BigDecimal(tsNtWebSiteConfIndexMap.get("25").getData_value());
                    BigDecimal studentChangeAmount = stu_amount.multiply(new BigDecimal(minute));
                    TdNtMember tdNtMember = tdNtLiveDao.findTdNtMember(tdNtStudyTeachUpdate.getMemb_id());
                    BigDecimal memb_banlance_back = tdNtMember.getMemb_balance().subtract(studentChangeAmount);
                    if (memb_banlance_back.compareTo(stu_amount.multiply(new BigDecimal(-1))) == 0) {
                        minute = minute - 1;
                    }

                    TdNtCoupon tdNtCoupon = new TdNtCoupon();
                    tdNtCoupon.setState(1);
                    tdNtCoupon.setType(2);
                    tdNtCoupon.setLimit_id(0);
                    tdNtCoupon.setMemb_id(tdNtStudyTeachUpdate.getMemb_id());
                    //查询用户是否有优惠券
                    TdNtCoupon coupon = tdNtLiveDao.findTdNtCoupon(tdNtCoupon);
                    if (coupon != null && coupon.getCoupon_amount().compareTo(new BigDecimal(minute)) >= 0) {
                        logger.info("使用学生"+ coupon.getMemb_id()+"的优惠券"+coupon.getCoupon_id()+"（不扣余额）");
                        coupon.setState(2);
                        tdNtLiveDao.updateTdNtCoupon(coupon);

                        /**
                         * 添加用户资金变动记录
                         */
                        TdNtMembFoundChangeInset tdNtMembFoundChangeInset = new TdNtMembFoundChangeInset();
                        tdNtMembFoundChangeInset.setMemb_id(tdNtStudyTeachUpdate.getMemb_id());
                        tdNtMembFoundChangeInset.setMfchg_back_amount(tdNtMember.getMemb_balance());
                        tdNtMembFoundChangeInset.setMfchg_change_amount(studentChangeAmount);
                        tdNtMembFoundChangeInset.setMfchg_change_month(CommonUtil.getMonth());
                        tdNtMembFoundChangeInset.setMfchg_front_amount(tdNtMember.getMemb_balance());
                        tdNtMembFoundChangeInset.setMfchg_change_name("呼叫老师（闲聊）[体验券抵扣]");
                        tdNtMembFoundChangeInset.setMfchg_change_type("2");
                        tdNtMembFoundChangeInset.setState("2");
                        tdNtMembFoundChangeInset.setType(3);//3呼叫老师(闲聊)
                        tdNtMembFoundChangeInset.setMfchg_number(CommonUtil.getOrderNub());
                        tdNtLiveDao.insertTdNtMembFoundChange(tdNtMembFoundChangeInset);
                    } else {
                        studentChangeAmount = stu_amount.multiply(new BigDecimal(minute));
                        tdNtStudyTeachUpdate.setStudy_amount(studentChangeAmount);
                        memb_banlance_back = tdNtMember.getMemb_balance().subtract(studentChangeAmount);
                        /**
                         * 添加学生资金变动记录
                         */
                        TdNtMembFoundChangeInset tdNtMembFoundChangeInset = new TdNtMembFoundChangeInset();
                        tdNtMembFoundChangeInset.setMemb_id(tdNtStudyTeachUpdate.getMemb_id());
                        tdNtMembFoundChangeInset.setMfchg_back_amount(memb_banlance_back);
                        tdNtMembFoundChangeInset.setMfchg_change_amount(studentChangeAmount);
                        tdNtMembFoundChangeInset.setMfchg_change_month(CommonUtil.getMonth());
                        tdNtMembFoundChangeInset.setMfchg_front_amount(tdNtMember.getMemb_balance());
                        tdNtMembFoundChangeInset.setMfchg_change_name("呼叫老师（闲聊）");
                        tdNtMembFoundChangeInset.setMfchg_change_type("2");
                        tdNtMembFoundChangeInset.setState("2");
                        tdNtMembFoundChangeInset.setType(3);
                        tdNtMembFoundChangeInset.setMfchg_number(CommonUtil.getOrderNub());
                        tdNtLiveDao.insertTdNtMembFoundChange(tdNtMembFoundChangeInset);
                        /**
                         * 修改学生余额，学生扣款
                         */
                        tdNtMember.setMemb_balance(tdNtMembFoundChangeInset.getMfchg_back_amount());
                        tdNtLiveDao.updateTdNtMemberBalance(tdNtMember);
                    }

                    tdNtTeaFoundChangeInsert.setTfchg_change_name(Constant.Teacher.FundChangeType.freetalk_msg);
                    tdNtTeaFoundChangeInsert.setType(Constant.Teacher.FundChangeType.freetalk_code);

                }
                //课程呼叫
                else {
                    String version = params.getMustParams().get("version");
                    int version_int = Integer.valueOf(version);
                    if (version_int > 6) {
                        tdNtTeaFoundChangeInsert.setTfchg_change_name(Constant.Teacher.FundChangeType.appointment_msg);
                        tdNtTeaFoundChangeInsert.setType(Constant.Teacher.FundChangeType.appointment_code);
                    }
//                        else {
//                            TdNtMembSetmealDetail tdNtMembSetmealDetail = tdNtLiveDao.findTdNtMembSetmealDetail(map);
//
//                            if (tdNtMembSetmealDetail != null && (tdNtMembSetmealDetail.getSurplus_time() + 1) >= month) {
//                                if ((tdNtMembSetmealDetail.getSurplus_time() + 1) == month) {
//                                    month = tdNtMembSetmealDetail.getSurplus_time();
//                                    tdNtStudyTeachUpdate.setStudy_time(month);
//                                }
//                                /**
//                                 * 更新套餐
//                                 */
//                                tdNtMembSetmealDetail.setSurplus_time(tdNtMembSetmealDetail.getSurplus_time() - month);
//                                if (tdNtMembSetmealDetail.getSet_limit() == 1 && tdNtMembSetmealDetail.getSurplus_time() <= 0) {
//                                    tdNtMembSetmealDetail.setState(3);
//                                    tdNtMembSetmealDetail.setEnd_time(new Timestamp(now));
//                                }
//                                tdNtLiveDao.udpateTdNtMembSetmealDetail(tdNtMembSetmealDetail);
//                                /**
//                                 * 添加套餐使用记录
//                                 */
//                                TdNtSetmealUseHisInsert tdNtSetmealUseHisInsert = new TdNtSetmealUseHisInsert();
//                                tdNtSetmealUseHisInsert.setEnd_time(tdNtStudyTeachUpdate.getEnd_time());
//                                tdNtSetmealUseHisInsert.setUse_time(tdNtStudyTeachUpdate.getStart_time());
//                                tdNtSetmealUseHisInsert.setSet_id(tdNtMembSetmealDetail.getSet_id());
//                                tdNtSetmealUseHisInsert.setStt_id(tdNtStudyTeachUpdate.getStt_id());
//                                tdNtSetmealUseHisInsert.setUser_duration(month);
//                                tdNtLiveDao.insertTdNtSetmealUseHis(tdNtSetmealUseHisInsert);
//
//                                tdNtStudyTeachUpdate.setStudy_amount(tdNtMembSetmealDetail.getMinute_amout().multiply(new BigDecimal(month)));
//
//                            }
//                        }

                }
//                }

                /**
                 * 更新学习记录
                 */
                tdNtLiveDao.updateTdNtStudyTeachEnd(tdNtStudyTeachUpdate);
                /**
                 * 添加教师资金变动记录
                 */
                BigDecimal changeAmount = tea_amount.multiply(new BigDecimal(tea_minute));

                tdNtTeaFoundChangeInsert.setState("2");
                tdNtTeaFoundChangeInsert.setTea_id(tdNtStudyTeachUpdate.getTea_id());
                tdNtTeaFoundChangeInsert.setTfchg_account(new BigDecimal("0"));
                tdNtTeaFoundChangeInsert.setTfchg_change_amount(changeAmount);
                tdNtTeaFoundChangeInsert.setTfchg_back_amount(tdNtTeacherIndex.getTea_balance().add(changeAmount));
                tdNtTeaFoundChangeInsert.setTfchg_change_month(CommonUtil.getMonth());
                tdNtTeaFoundChangeInsert.setTfchg_change_type("1");
                tdNtTeaFoundChangeInsert.setTfchg_counter_fee(new BigDecimal("0"));
                tdNtTeaFoundChangeInsert.setTfchg_front_amount(tdNtTeacherIndex.getTea_balance());
                tdNtTeaFoundChangeInsert.setTfchg_number(CommonUtil.getOrderNub());
                tdNtLiveDao.insertTdNtTeaFoundChangeInsert(tdNtTeaFoundChangeInsert);
                /**
                 * 修改教师余额 及教学时间
                 */
                tdNtTeacherIndex.setTea_balance(tdNtTeaFoundChangeInsert.getTfchg_back_amount());
                tdNtTeacherIndex.setTea_times(tdNtTeacherIndex.getTea_times() + tea_minute);
                tdNtTeacherIndex.setTea_id(tdNtStudyTeachUpdate.getTea_id());
                tdNtTeacherIndex.setTea_total_balance(tdNtTeacherIndex.getTea_total_balance().add(tdNtTeaFoundChangeInsert.getTfchg_change_amount()));
                tdNtTeacherIndex.setTea_total_times(tdNtTeacherIndex.getTea_total_times() + tea_minute);
                tdNtLiveDao.updateTdNtTeacherIndex(tdNtTeacherIndex);
                /**
                 * 更新老师状态为在线
                 */
                TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
                tdNtTeacherState.setTea_id(tdNtStudyTeachUpdate.getTea_id());
                tdNtTeacherState.setState(Constant.Teacher.State.online);
                tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.online);
                tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);

                /**
                 * 添加或者更新用户学习课程记录
                 */
//                if (!Constant.freetalk_cou_id.equals(tdNtStudyTeachUpdate.getCou_id().toString())) {
                if (MemcachedUtils.get(stt_id + "stt_id") != null) {
                    Integer size = (Integer) MemcachedUtils.get(stt_id + "stt_id");
                    Integer study_page = size;
                    TdNtMembCourseDetail tdNtMembCourseDetail = new TdNtMembCourseDetail();
                    tdNtMembCourseDetail.setMemb_id(tdNtStudyTeachUpdate.getMemb_id());
                    tdNtMembCourseDetail.setCou_id(tdNtStudyTeachUpdate.getCou_id());
                    tdNtMembCourseDetail = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourseDetail);

                    if (tdNtMembCourseDetail != null) {
                        if (tdNtMembCourseDetail.getState().equals("2")) {
                            TdNtMembCourseDetail membCourseDetail = new TdNtMembCourseDetail();
                            membCourseDetail.setCou_id(tdNtStudyTeachUpdate.getCou_id());
                            membCourseDetail.setMemb_id(tdNtStudyTeachUpdate.getMemb_id());
                            membCourseDetail.setState("1");
                            membCourseDetail.setStudy_page(study_page);
                            tdNtLiveDao.insertTdNtMembCourse(membCourseDetail);
                        } else {
                            Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                            List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get("" + tdNtStudyTeachUpdate.getCou_id());
                            tdNtMembCourseDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                            tdNtMembCourseDetail.setStudy_page(study_page);
                            if (tdNtCoursePictureLists.size() == study_page) {
                                tdNtMembCourseDetail.setState("2");
                                /**
                                 * 更新课程学习人数
                                 */
                                Map<String, Object> updateCourse = new HashMap<>();
                                tsNtCourseInfoDetail.setStudy_size(tsNtCourseInfoDetail.getStudy_size() + 1);
                                updateCourse.put("cou_id", tdNtStudyTeachUpdate.getCou_id());
                                updateCourse.put("study_size", tsNtCourseInfoDetail.getStudy_size());
                                tdNtLiveDao.updateTsNtCourseInfoStudySize(updateCourse);
                            }
                            tdNtLiveDao.updateTdNtMembCourse(tdNtMembCourseDetail);
                        }
                    } else {
                        TdNtMembCourseDetail membCourseDetail = new TdNtMembCourseDetail();
                        membCourseDetail.setCou_id(tdNtStudyTeachUpdate.getCou_id());
                        membCourseDetail.setMemb_id(tdNtStudyTeachUpdate.getMemb_id());
                        membCourseDetail.setState("1");
                        membCourseDetail.setStudy_page(study_page);
                        tdNtLiveDao.insertTdNtMembCourse(membCourseDetail);

                    }
//                    }
                }
                TdNtMemberStudyTimes tdNtMemberStudyTimes = tdNtLiveDao.findTdNtMemberStudyTimes(tdNtStudyTeachUpdate.getMemb_id());
                tdNtMemberStudyTimes.setTotal_study_times(tdNtMemberStudyTimes.getTotal_study_times() + minute);
                tdNtLiveDao.updateTdNtMemberStudyTimes(tdNtMemberStudyTimes);
                retInfo.setMark("0");
                retInfo.setTip("结束成功");
                Map<String, Object> result = new HashMap<>();
                result.put("type", "endOrders");
                retInfo.setObj(result);
            } else {
                retInfo.setMark("1");
                retInfo.setTip("权限不足");
                Map<String, Object> result = new HashMap<>();
                result.put("type", "endOrders");
                retInfo.setObj(result);
            }

        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            Map<String, Object> result = new HashMap<>();
            result.put("type", "endOrders");
            retInfo.setObj(result);
            logger.error("json数据错误"+e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * @param params
     * @param channel
     * @return
     * @throws Exception
     * @Purpose 学生呼叫老师 预约课
     */
    @Override
    public RetInfo reserveStudentKall(Params params, Channel channel) throws Exception {
        String logInfo = this.getClass().getName() + ":reserveKall:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        String reserve_id = params.getParams().get("reserve_id").toString();
        String conference_title = params.getParams().get("conference_title").toString();
        String conference_url = params.getParams().get("conference_url").toString();
        String conference_password = params.getParams().get("conference_password").toString();

        TdNtReserve tdNtReserve = tdNtLiveDao.findTdNtReserveById(Integer.valueOf(reserve_id));
        String cou_id = tdNtReserve.getCou_id() + "";
        String tea_id = tdNtReserve.getTea_id() + "";
        TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
        TdNtTeacherGetui tdNtTeacherGetui = tdNtLiveDao.findTdNtTeacherByTeaId(Integer.valueOf(tea_id));
        /**
         * 判断老师是否存在及是否在线
         */
        if (tdNtTeacherGetui != null && MemcachedUtils.get(tdNtTeacherGetui.getToken()) != null) {
            TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
            Map<String, TwoTsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TwoTsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMapTwo);
            TwoTsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
            try {
                //判断课程是否可用
                if (tsNtCourseInfoDetail != null) {
                    //添加学习记录
                    tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                    tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                    tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                    tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                    tdNtStudyTeachInsert.setStudy_cou_picture(tsNtCourseInfoDetail.getStand());
                    tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                    tdNtStudyTeachInsert.setStudy_state("0");
                    tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachInsert.setLaunch_time(timestamp);
                    tdNtStudyTeachInsert.setConference_title(conference_title);
                    tdNtStudyTeachInsert.setConference_url(conference_url);
                    tdNtStudyTeachInsert.setConference_password(conference_password);
                    tdNtLiveDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);

                    //添加预约上课历史记录
                    TdNtReserveHistory tdNtReserveHistory = new TdNtReserveHistory();
                    tdNtReserveHistory.setReserve_id(tdNtReserve.getReserve_id());
                    tdNtReserveHistory.setStt_id(tdNtStudyTeachInsert.getStt_id());
                    tdNtReserveHistory.setReserve_his_start_time(new Timestamp(System.currentTimeMillis()));
                    tdNtReserveHistory.setUser_type("student");
                    tdNtReserveHistory.setReserve_his_time(0);
                    tdNtLiveDao.insertTdNtReserveHistory(tdNtReserveHistory);
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("课程不存在");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    return retInfo;
                }


            } catch (Exception e) {
                retInfo.setMark("-1");
                retInfo.setTip("json数据错误");
                Map<String, Object> result = new HashMap<>();
                result.put("type", params.getFunctionName());
                retInfo.setObj(result);
                logger.error(e.getMessage(), e);
            } finally {
                TdNtMembCourseDetail tdNtMembCourse = new TdNtMembCourseDetail();
                tdNtMembCourse.setCou_id(Integer.valueOf(cou_id));
                tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
                tdNtMembCourse = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourse);
                CallTeacher callTeacher = new CallTeacher();
                //启动伪线程——！ **** ********************************************************************************************
                callTeacher.run(tdNtLiveDao, tdNtStudyTeachInsert.getStt_id(),
                        tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(), tsNtCourseInfoDetail.getCou_english_name(), tdNtTeacherGetui, channel);

                //查询教学记录
                TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtLiveDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
                //0找寻老师中1学习中2待支付3待评价4已完成5已完成6已取消7老师拒绝
                if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                    TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                    Map<String, Object> map = new HashMap<>();
                    if (tdNtMembCourse == null) {
                        map.put("studySize", 1);
                    } else {
                        map.put("studySize", tdNtMembCourse.getStudy_page());
                    }
                    //剩余的学习时间
                    map.put("surplus_time", tdNtReserve.getReserve_time() - tdNtLiveDao.findReserveTime(Integer.valueOf(reserve_id)));
                    TdNtMember member = tdNtLiveDao.findTdNtMember(tdNtMember.getMemb_id());
                    map.put("memb_name", member.getMemb_name());
                    map.put("memb_head_portrait", member.getMemb_head_portrait());
                    map.put("video_switch", member.getVideo_switch());

                    map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                    map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                    map.put("tea_name", tdNtTeacherIndex.getTea_name());
                    map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                    Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);
                    map.put("course_pictures", tdNtCoursePictureLists);
                    /**
                     * 每个课程的每一章节开始对应的页数列表
                     */
                    Map<String, List<TdNtCourseLevelPageList>> tdNtCourseLevelPageListMap = (Map<String, List<TdNtCourseLevelPageList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelPageListMap);
                    List<TdNtCourseLevelPageList> tdNtCourseLevelPageLists = tdNtCourseLevelPageListMap.get(cou_id);
                    //该课程没有配章节
                    if (null == tdNtCourseLevelPageLists) {
                        tdNtCourseLevelPageLists = new ArrayList<>();
                        TdNtCourseLevelPageList tdNtCourseLevelPageList = new TdNtCourseLevelPageList();
                        tdNtCourseLevelPageList.setCou_id(100);
                        tdNtCourseLevelPageList.setStart_page(1);
                        tdNtCourseLevelPageList.setEnd_page(tdNtCoursePictureLists.size());
                        tdNtCourseLevelPageList.setLevel_sort(1);
                        tdNtCourseLevelPageList.setLevel_english_name("");
                        tdNtCourseLevelPageLists.add(tdNtCourseLevelPageList);
                    }
                    map.put("course_level_page", tdNtCourseLevelPageLists);

                    String study_channel = tdNtTeacherIndex.getToken() + tdNtStudyTeachInsert.getStt_id();
                    tdNtStudyTeachInsert.setChannel(study_channel);
                    tdNtLiveDao.updateTdNtStudyTeachChannel(tdNtStudyTeachInsert);
                    map.put("channel", study_channel);

                    map.put("recordKey", DynamicKey4Sample.getRecordKey(appID, study_channel, ts, r, uid, expiredTs));
                    map.put("channelKey", DynamicKey4Sample.getChannelKey(appID, study_channel, ts, r, uid, expiredTs));
                    map.put("ts", ts);
                    map.put("r", r);
                    map.put("uid", uid);
                    map.put("appID", appID);
                    map.put("expiredTs", expiredTs);
                    map.put("type", "kallTeacer");

                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                    retInfo.setObj(map);
                } else if (tdNtStudyTeachDetail.getStudy_state().equals("7")) {
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    retInfo.setMark("1");
                    retInfo.setTip("老师拒绝");
                    retInfo.setObj(result);
                } else {
                    tdNtStudyTeachDetail.setStudy_state("6");
                    tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                    tdNtLiveDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                    retInfo.setMark("1");
                    retInfo.setTip("取消成功");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    /**
                     * 取消发送推送
                     */
                    if (tdNtTeacherGetui.getEqu_type().equals("1")) {
                        AndroidTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());
                    } else {
                        IosTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());

                    }
                }

            }
        } else {
            retInfo.setMark("1");
            retInfo.setTip("对不起,您呼叫的老师不在线!");
            Map<String, Object> result = new HashMap<>();
            result.put("type", params.getFunctionName());
            retInfo.setObj(result);
            /**
             * 将老师修改为不在线
             */
            TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
            tdNtTeacherState.setTea_id(Integer.valueOf(tea_id));
            tdNtTeacherState.setState(Constant.Teacher.State.offline);
            tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.offline);
            tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo teacherRefuse(Params params, Channel channel) {
        String logInfo = this.getClass().getName() + ":teacherRefuse:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        Map<String, Object> result = new HashMap<>();
        result.put("type", params.getFunctionName());
        try {
            int stt_id = Integer.valueOf(params.getParams().get("stt_id").toString());
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(params.getMustParams().get("token").toString());
            Map<String, Integer> map = new HashMap<>();
            map.put("stt_id", stt_id);
            map.put("tea_id", tdNtTeacherResult.getTea_id());
            tdNtLiveDao.updateTdNtStudyTeachTeacher(map);
            retInfo.setMark("0");
            retInfo.setTip("拒绝成功");
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(), e);
        }
        retInfo.setObj(result);
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo reserveTeacherKall(Params params, Channel channel) throws Exception {
        String logInfo = this.getClass().getName() + ":reserveKall:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        String reserve_id = params.getParams().get("reserve_id").toString();
        String conference_title = params.getParams().get("conference_title").toString();
        String conference_url = params.getParams().get("conference_url").toString();
        String conference_password = params.getParams().get("conference_password").toString();

        TdNtReserve tdNtReserve = tdNtLiveDao.findTdNtReserveById(Integer.valueOf(reserve_id));
        String cou_id = tdNtReserve.getCou_id() + "";
        String tea_id = tdNtReserve.getTea_id() + "";
        TdNtTeacherResult teacher = (TdNtTeacherResult) MemcachedUtils.get(params.getMustParams().get("token"));
        TdNtTeacherGetui tdNtTeacherGetui = tdNtLiveDao.findTdNtTeacherByTeaId(Integer.valueOf(tea_id));
        TdNtMember tdNtMember = tdNtLiveDao.findTdNtMember(tdNtReserve.getMemb_id());
        /**
         * 判断老师是否存在及是否在线
         */
        if (tdNtMember != null && MemcachedUtils.get(tdNtMember.getToken()) != null) {
            TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
            Map<String, TwoTsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TwoTsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMapTwo);
            TwoTsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
            try {
                //判断课程是否可用
                if (tsNtCourseInfoDetail != null) {

                    tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                    tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                    tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                    tdNtStudyTeachInsert.setStudy_cou_picture(tsNtCourseInfoDetail.getStand());
                    tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                    tdNtStudyTeachInsert.setStudy_state("0");
                    tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachInsert.setLaunch_time(timestamp);
                    tdNtStudyTeachInsert.setTea_id(teacher.getTea_id());
                    tdNtStudyTeachInsert.setConference_title(conference_title);
                    tdNtStudyTeachInsert.setConference_url(conference_url);
                    tdNtStudyTeachInsert.setConference_password(conference_password);
                    tdNtLiveDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                    TdNtReserveHistory tdNtReserveHistory = new TdNtReserveHistory();
                    tdNtReserveHistory.setReserve_id(tdNtReserve.getReserve_id());
                    tdNtReserveHistory.setStt_id(tdNtStudyTeachInsert.getStt_id());
                    tdNtReserveHistory.setReserve_his_start_time(new Timestamp(System.currentTimeMillis()));
                    tdNtReserveHistory.setUser_type("teacher");
                    tdNtReserveHistory.setReserve_his_time(0);
                    tdNtLiveDao.insertTdNtReserveHistory(tdNtReserveHistory);
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("课程不存在");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    return retInfo;
                }


            } catch (Exception e) {
                retInfo.setMark("-1");
                retInfo.setTip("json数据错误");
                Map<String, Object> result = new HashMap<>();
                result.put("type", params.getFunctionName());
                retInfo.setObj(result);
                logger.error(e.getMessage(), e);
            } finally {

                TdNtMembCourseDetail tdNtMembCourse = new TdNtMembCourseDetail();
                tdNtMembCourse.setCou_id(Integer.valueOf(cou_id));
                tdNtMembCourse.setMemb_id(tdNtReserve.getMemb_id());
                tdNtMembCourse = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourse);
                CallStudent callStudent = new CallStudent();
                callStudent.run(tdNtLiveDao, tdNtStudyTeachInsert.getStt_id(),
                        teacher.getTea_name(), teacher.getTea_head_portrait(), tsNtCourseInfoDetail.getCou_name(), tdNtMember, teacher.getToken(), channel, tdNtReserve.getReserve_time() - tdNtLiveDao.findReserveTime(Integer.valueOf(reserve_id)));
                TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtLiveDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
                if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                    Map<String, Object> map = new HashMap<>();
                    if (tdNtMembCourse == null) {
                        map.put("studySize", 1);
                    } else {
                        map.put("studySize", tdNtMembCourse.getStudy_page());
                    }
                    TdNtMember member = tdNtLiveDao.findTdNtMember(tdNtMember.getMemb_id());
                    map.put("memb_name", member.getMemb_name());
                    map.put("memb_head_portrait", member.getMemb_head_portrait());
                    map.put("video_switch", member.getVideo_switch());

                    map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                    map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                    map.put("tea_name", teacher.getTea_name());
                    map.put("tea_head_portrait", teacher.getTea_head_portrait());
                    Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);
                    map.put("course_pictures", tdNtCoursePictureLists);

                    /**
                     * 每个课程的每一章节开始对应的页数列表
                     */
                    Map<String, List<TdNtCourseLevelPageList>> tdNtCourseLevelPageListMap = (Map<String, List<TdNtCourseLevelPageList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelPageListMap);
                    List<TdNtCourseLevelPageList> tdNtCourseLevelPageLists = tdNtCourseLevelPageListMap.get(cou_id);
                    //该课程没有配章节
                    if (null == tdNtCourseLevelPageLists) {
                        tdNtCourseLevelPageLists = new ArrayList<>();
                        TdNtCourseLevelPageList tdNtCourseLevelPageList = new TdNtCourseLevelPageList();
                        tdNtCourseLevelPageList.setCou_id(100);
                        tdNtCourseLevelPageList.setStart_page(1);
                        tdNtCourseLevelPageList.setEnd_page(tdNtCoursePictureLists.size());
                        tdNtCourseLevelPageList.setLevel_sort(1);
                        tdNtCourseLevelPageList.setLevel_english_name("");
                        tdNtCourseLevelPageLists.add(tdNtCourseLevelPageList);
                    }
                    map.put("course_level_page", tdNtCourseLevelPageLists);

                    String study_channel = teacher.getToken() + tdNtStudyTeachInsert.getStt_id();
                    tdNtStudyTeachInsert.setChannel(study_channel);
                    tdNtLiveDao.updateTdNtStudyTeachChannel(tdNtStudyTeachInsert);
                    map.put("channel", study_channel);

                    map.put("recordKey", DynamicKey4Sample.getRecordKey(appID, study_channel, ts, r, uid, expiredTs));
                    map.put("channelKey", DynamicKey4Sample.getChannelKey(appID, study_channel, ts, r, uid, expiredTs));
                    map.put("ts", ts);
                    map.put("r", r);
                    map.put("uid", uid);
                    map.put("appID", appID);

                    map.put("expiredTs", expiredTs);
                    map.put("type", "kallTeacer");
                    map.put("student_channel", tdNtMember.getToken());

                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                    retInfo.setObj(map);
                } else if (tdNtStudyTeachDetail.getStudy_state().equals("7")) {
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    retInfo.setMark("1");
                    retInfo.setTip("学生拒绝");
                    retInfo.setObj(result);

                } else {

                    tdNtStudyTeachDetail.setStudy_state("6");
                    tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                    tdNtLiveDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                    retInfo.setMark("1");
                    retInfo.setTip("取消成功");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", params.getFunctionName());
                    retInfo.setObj(result);
                    /**
                     * 取消发送推送
                     */
                    if (tdNtMember.getEqu_type().equals("1")) {
                        AndroidStudentGetui.Cancel("外教君", "老师取消",
                                tdNtMember.getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());
                    } else {
                        IosStudentGetui.Cancel("外教君", "老师取消",
                                tdNtMember.getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());

                    }
                }

            }
        } else {
            retInfo.setMark("1");
            retInfo.setTip("Sorry, the student you are calling is not online.");
            Map<String, Object> result = new HashMap<>();
            result.put("type", params.getFunctionName());
            retInfo.setObj(result);
            /**
             * 将老师修改为忙碌
             */
            TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
            tdNtTeacherState.setTea_id(Integer.valueOf(tea_id));
            tdNtTeacherState.setState(Constant.Teacher.State.busy);
            tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.busy);
            tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo studentRefuse(Params params, Channel channel) {
        String logInfo = this.getClass().getName() + ":studentRefuse:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        Map<String, Object> result = new HashMap<>();
        result.put("type", params.getFunctionName());
        try {
            int stt_id = Integer.valueOf(params.getParams().get("stt_id").toString());
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token").toString());
            Map<String, Integer> map = new HashMap<>();
            map.put("stt_id", stt_id);
            map.put("memb_id", tdNtMember.getMemb_id());
            tdNtLiveDao.updateTdNtStudyTeachStudent(map);
            retInfo.setMark("0");
            retInfo.setTip("拒绝成功");
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(), e);
        }
        retInfo.setObj(result);
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * 学生接听电话
     *
     * @param params
     * @return
     */
    @Override
    public RetInfo eceivingStudentOrders(Params params) {
        String logInfo = this.getClass().getName() + ":eceivingStudentOrders:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            uid = 28823L;
            int stt_id = Integer.valueOf(params.getParams().get("stt_id").toString());
            TdNtStudyTeachUpdate tdNtStudyTeachUpdate = tdNtLiveDao.findTdNtStudyTeachUpdate(stt_id);
            if (tdNtStudyTeachUpdate != null && tdNtStudyTeachUpdate.getStudy_state().equals("0")) {

                TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
                tdNtStudyTeachUpdate.setStudy_state("1");
                tdNtStudyTeachUpdate.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachUpdate.setStart_time(new Timestamp(System.currentTimeMillis()));

                tdNtLiveDao.updateTdNtStudyTeach(tdNtStudyTeachUpdate);

                TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
                tdNtTeacherState.setTea_id(tdNtStudyTeachUpdate.getTea_id());
                tdNtTeacherState.setState(Constant.Teacher.State.offline);
                tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.offline);
                TdNtMemberDetailOrders tdNtMemberDetailOrders = tdNtLiveDao.findTdNtMemberDetailOrders(tdNtStudyTeachUpdate.getMemb_id());
                tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
                retInfo.setMark("0");
                retInfo.setTip("接单成功");
                Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get("" + tdNtStudyTeachUpdate.getCou_id());
//                    retInfo.setObj(tdNtCoursePictureLists);

                //获取当前课程学习页数
                TdNtMembCourseDetail tdNtMembCourse = new TdNtMembCourseDetail();
                tdNtMembCourse.setCou_id(tdNtStudyTeachUpdate.getCou_id());
                tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
                tdNtMembCourse = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourse);
                Map<String, Object> map = new HashMap<>();
                if (Constant.freetalk_cou_id.equals(tdNtStudyTeachUpdate.getCou_id().toString())) {
                    map.put("video_switch", 0);
                } else {
                    map.put("video_switch", tdNtMemberDetailOrders.getVideo_switch());
                }
                if (tdNtMembCourse == null) {
                    map.put("studySize", 1);
                } else {
                    map.put("studySize", tdNtMembCourse.getStudy_page());
                }
                map.put("course_pictures", tdNtCoursePictureLists);
                map.put("student_channel", tdNtMemberDetailOrders.getToken());
                int ts = (int) tdNtStudyTeachUpdate.getLaunch_time().getTime() / 1000;
                map.put("channel", tdNtStudyTeachUpdate.getChannel());
                map.put("recordKey", DynamicKey4Sample.getRecordKey(appID, tdNtStudyTeachUpdate.getChannel(), ts, r, uid, expiredTs));
                map.put("channelKey", DynamicKey4Sample.getChannelKey(appID, tdNtStudyTeachUpdate.getChannel(), ts, r, uid, expiredTs));
                map.put("ts", ts);
                map.put("r", r);
                map.put("uid", uid);
                map.put("appID", appID);
                map.put("expiredTs", expiredTs);
                map.put("type", params.getFunctionName());
                map.put("conference_title", tdNtStudyTeachUpdate.getConference_title());
                map.put("conference_url", tdNtStudyTeachUpdate.getConference_url());
                map.put("conference_password", tdNtStudyTeachUpdate.getConference_password());
                retInfo.setObj(map);

            }
            /**
             * 将老师修改为忙碌
             */
            TdNtTeacherState tdNtTeacherState = new TdNtTeacherState();
            tdNtTeacherState.setTea_id(tdNtStudyTeachUpdate.getTea_id());
            tdNtTeacherState.setState(Constant.Teacher.State.busy);
            tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.busy);
            tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            Map<String, Object> result = new HashMap<>();
            result.put("type", params.getFunctionName());
            retInfo.setObj(result);
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public TdNtMember findTdNtMemberBySttId(Params params) {
        String stt_id = params.getParams().get("stt_id").toString();
        return tdNtLiveDao.findTdNtMemberBySttId(Integer.valueOf(stt_id));
    }

    @Override
    public List<TdNtMember> findTdNtMemberByLive(int live_cou_his_id) {
        return tdNtLiveDao.findTdNtMemberByLive(live_cou_his_id);
    }

    @Override
    public TdNtTeacherResult findTdNtTeacher(int live_cou_his_id) {
        return tdNtLiveDao.findTdNtTeacher(live_cou_his_id);
    }

    @Override
    public synchronized RetInfo givePurchase(Params params) {
        String logInfo = this.getClass().getName() + ":givePurchase:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtLiveGivingList> tsNtLiveGivingListMap = (Map<String, TsNtLiveGivingList>) MemcachedUtils.get(MemcachedKey.tsNtLiveGivingListsMap);
            TdNtMember member = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
            TdNtMember tdNtMember = tdNtLiveDao.findTdNtMember(member.getMemb_id());
            Map<String, Object> map = params.getParams();
            TsNtLiveGivingList tsNtLiveGivingList = tsNtLiveGivingListMap.get(map.get("giv_id").toString());
            if (tdNtMember.getMemb_purchase_balance().compareTo(tsNtLiveGivingList.getGiv_amount()) > -1) {
                TdNtLiveGivingHistory tdNtLiveGivingHistory = new TdNtLiveGivingHistory();
                tdNtLiveGivingHistory.setGiv_amount(tsNtLiveGivingList.getGiv_amount());
                tdNtLiveGivingHistory.setLive_his_id(Integer.valueOf(map.get("live_cou_his_id").toString()));
                tdNtLiveGivingHistory.setGiv_icon(tsNtLiveGivingList.getGiv_icon());
                tdNtLiveGivingHistory.setGiv_id(tsNtLiveGivingList.getGiv_id());
                tdNtLiveGivingHistory.setGiv_name(tsNtLiveGivingList.getGiv_name());
                tdNtLiveGivingHistory.setGiv_popularity(tsNtLiveGivingList.getGiv_popularity());
                tdNtLiveGivingHistory.setHis_month(CommonUtil.getMonth());
                tdNtLiveGivingHistory.setMemb_id(tdNtMember.getMemb_id());
                tdNtLiveGivingHistory.setTea_id(Integer.valueOf(map.get("tea_id").toString()));
                tdNtLiveGivingHistory.setGiv_us_name(tsNtLiveGivingList.getGiv_us_name());
                tdNtLiveGivingHistory.setGiv_teacher_amount(tsNtLiveGivingList.getGiv_teacher_amount());
                tdNtLiveDao.insertTdNtLiveGivingHistory(tdNtLiveGivingHistory);
                /**
                 * 添加资金变动记录 更新用户内购余额
                 */
                TdNtMembPurchaseChange tdNtMembPurchaseChange = new TdNtMembPurchaseChange();
                tdNtMembPurchaseChange.setMemb_id(tdNtMember.getMemb_id());
                tdNtMembPurchaseChange.setMfchg_change_amount(tsNtLiveGivingList.getGiv_amount());
                tdNtMembPurchaseChange.setMfchg_change_month(CommonUtil.getMonth());
                tdNtMembPurchaseChange.setMfchg_change_name("购买" + tsNtLiveGivingList.getGiv_name());
                tdNtMembPurchaseChange.setMfchg_change_type("2");
                tdNtMembPurchaseChange.setState("2");
                tdNtMembPurchaseChange.setMfchg_number(CommonUtil.getOrderNub());
                tdNtMembPurchaseChange.setMfchg_back_amount(tdNtMember.getMemb_purchase_balance().subtract(tsNtLiveGivingList.getGiv_amount()));
                tdNtMembPurchaseChange.setMfchg_front_amount(tdNtMember.getMemb_purchase_balance());
                tdNtLiveDao.insertTdNtMembPurchaseChange(tdNtMembPurchaseChange);
                tdNtMember.setMemb_purchase_balance(tdNtMember.getMemb_purchase_balance().subtract(tsNtLiveGivingList.getGiv_amount()));
                tdNtLiveDao.updateMemberPurchase(tdNtMember);
                /**
                 * 添加教师资金变动记录, 更新教师余额
                 */
                /**
                 * 添加教师资金变动记录
                 */
                TdNtTeacherIndex tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherIndex(tdNtLiveGivingHistory.getTea_id());
                TdNtTeaFoundChangeInsert tdNtTeaFoundChangeInsert = new TdNtTeaFoundChangeInsert();
                tdNtTeaFoundChangeInsert.setState("2");
                tdNtTeaFoundChangeInsert.setTea_id(tdNtLiveGivingHistory.getTea_id());
                tdNtTeaFoundChangeInsert.setTfchg_account(new BigDecimal("0"));
                tdNtTeaFoundChangeInsert.setTfchg_change_amount(tdNtLiveGivingHistory.getGiv_teacher_amount());
                tdNtTeaFoundChangeInsert.setTfchg_back_amount(tdNtTeacherIndex.getTea_balance().add(tdNtLiveGivingHistory.getGiv_teacher_amount()));
                tdNtTeaFoundChangeInsert.setTfchg_change_month(CommonUtil.getMonth());
                tdNtTeaFoundChangeInsert.setTfchg_change_type("1");
                tdNtTeaFoundChangeInsert.setTfchg_counter_fee(new BigDecimal("0"));
                tdNtTeaFoundChangeInsert.setTfchg_front_amount(tdNtTeacherIndex.getTea_balance());
                tdNtTeaFoundChangeInsert.setTfchg_number(CommonUtil.getOrderNub());
                tdNtTeaFoundChangeInsert.setTfchg_change_name(tdNtLiveGivingHistory.getGiv_us_name());
                tdNtTeaFoundChangeInsert.setType(3);
                tdNtLiveDao.insertTdNtTeaFoundChangeInsert(tdNtTeaFoundChangeInsert);
                /**
                 * 修改教师余额
                 */
                tdNtTeacherIndex.setTea_balance(tdNtTeaFoundChangeInsert.getTfchg_back_amount());
                tdNtTeacherIndex.setTea_id(tdNtLiveGivingHistory.getTea_id());
                tdNtTeacherIndex.setTea_total_balance(tdNtTeacherIndex.getTea_total_balance().add(tdNtTeaFoundChangeInsert.getTfchg_change_amount()));
                tdNtLiveDao.updateTdNtTeacherIndex(tdNtTeacherIndex);
                Object object = MemcachedUtils.get("live" + map.get("live_cou_his_id"));
                int a = tdNtLiveGivingHistory.getGiv_popularity();
                if (object != null) {
                    a = Integer.valueOf(object.toString()) + a;
                    MemcachedUtils.replace("live" + map.get("live_cou_his_id"), a, 2 * 60 * 60 * 1000);
                } else {

                    MemcachedUtils.add("live" + map.get("live_cou_his_id"), a, 2 * 60 * 60 * 1000);
                }
                Map<String, String> result = new HashMap<>();
                result.put("memb_name", tdNtMember.getMemb_name());
                result.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                result.put("giv_id", tsNtLiveGivingList.getGiv_id() + "");
                result.put("type", "givePurchase");
                result.put("popularity", a + "");
                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setObj(result);
            } else {
                retInfo.setMark("1");
                retInfo.setTip("余额不足");
            }
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo randomFreeKallTeacer(Params params, Channel channel) throws Exception {
        String logInfo = this.getClass().getName() + ":randomFreeKallTeacer:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        String cou_id = Constant.freetalk_cou_id;
        String conference_title = params.getParams().get("conference_title").toString();
        String conference_url = params.getParams().get("conference_url").toString();
        String conference_password = params.getParams().get("conference_password").toString();

        TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
        Map<String, TwoTsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TwoTsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMapTwo);
        TwoTsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
        TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
        int time = 0;
        try {
            //判断课程是否可用
            if (tsNtCourseInfoDetail != null) {
                //查看用户是否有余额及优惠分钟数
                TdNtMember member = tdNtLiveDao.findTdNtMember(tdNtMember.getMemb_id());
                TdNtCoupon tdNtCoupon = new TdNtCoupon();
                tdNtCoupon.setState(1);
                tdNtCoupon.setType(2);
                tdNtCoupon.setLimit_id(0);
                tdNtCoupon.setMemb_id(tdNtMember.getMemb_id());
                TdNtCoupon coupon = tdNtLiveDao.findTdNtCoupon(tdNtCoupon);
                int balance_time = member.getMemb_balance().divide(new BigDecimal("1.69"), 1).intValue();
                if (coupon == null && balance_time < 1) {

                    retInfo.setMark("1");
                    retInfo.setTip("余额不足");
                    Map<String, Object> result = new HashMap<>();
                    result.put("type", "randomFreeKallTeacer");
                    retInfo.setObj(result);
                    return retInfo;
                } else {
                    if (coupon != null) {
                        time = coupon.getCoupon_amount().intValue();
                    } else {
                        time = balance_time;
                    }
                }
                tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                tdNtStudyTeachInsert.setStudy_cou_picture(tsNtCourseInfoDetail.getStand());
                tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                tdNtStudyTeachInsert.setStudy_state("0");
                tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachInsert.setLaunch_time(timestamp);
                tdNtStudyTeachInsert.setConference_title(conference_title);
                tdNtStudyTeachInsert.setConference_url(conference_url);
                tdNtStudyTeachInsert.setConference_password(conference_password);
                tdNtLiveDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);

            } else {
                retInfo.setMark("1");
                retInfo.setTip("课程不存在");
                Map<String, Object> result = new HashMap<>();
                result.put("type", "randomFreeKallTeacer");
                retInfo.setObj(result);
                return retInfo;
            }


        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            Map<String, Object> result = new HashMap<>();
            result.put("type", "randomKallTeacer");
            retInfo.setObj(result);
            logger.error(e.getMessage(), e);
        } finally {
            TdNtMembCourseDetail tdNtMembCourse = new TdNtMembCourseDetail();
            tdNtMembCourse.setCou_id(Integer.valueOf(cou_id));
            tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
            tdNtMembCourse = tdNtLiveDao.findTdNtMembCourseDetail(tdNtMembCourse);
            FindFreeTeacher findTeacher = new FindFreeTeacher();
            findTeacher.run(tdNtLiveDao, tdNtStudyTeachInsert.getStt_id()
                    , tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(), tsNtCourseInfoDetail.getCou_english_name(), channel);
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtLiveDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
            if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtLiveDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                Map<String, Object> map = new HashMap<>();
                if (tdNtMembCourse == null) {
                    map.put("studySize", 1);
                } else {
                    map.put("studySize", tdNtMembCourse.getStudy_page());
                }
                map.put("surplus_time", time);
                Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
                double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
                map.put("sutdy_amount", sutdy_amount);
                TdNtMember member = tdNtLiveDao.findTdNtMember(tdNtMember.getMemb_id());
                map.put("memb_name", member.getMemb_name());
                map.put("memb_head_portrait", member.getMemb_head_portrait());
                map.put("video_switch", member.getVideo_switch());

                map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                map.put("tea_name", tdNtTeacherIndex.getTea_name());
                map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                Map<String, List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);

                map.put("course_pictures", tdNtCoursePictureLists);

                /**
                 * 每个课程的每一章节开始对应的页数列表
                 */
                Map<String, List<TdNtCourseLevelPageList>> tdNtCourseLevelPageListMap = (Map<String, List<TdNtCourseLevelPageList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelPageListMap);
                List<TdNtCourseLevelPageList> tdNtCourseLevelPageLists = tdNtCourseLevelPageListMap.get(cou_id);
                //该课程没有配章节
                if (null == tdNtCourseLevelPageLists) {
                    tdNtCourseLevelPageLists = new ArrayList<>();
                    TdNtCourseLevelPageList tdNtCourseLevelPageList = new TdNtCourseLevelPageList();
                    tdNtCourseLevelPageList.setCou_id(100);
                    tdNtCourseLevelPageList.setStart_page(1);
                    tdNtCourseLevelPageList.setEnd_page(tdNtCoursePictureLists.size());
                    tdNtCourseLevelPageList.setLevel_sort(1);
                    tdNtCourseLevelPageList.setLevel_english_name("");
                    tdNtCourseLevelPageLists.add(tdNtCourseLevelPageList);
                }
                map.put("course_level_page", tdNtCourseLevelPageLists);

                String study_channel = tdNtTeacherIndex.getToken() + tdNtStudyTeachInsert.getStt_id();
                tdNtStudyTeachInsert.setChannel(study_channel);
                tdNtLiveDao.updateTdNtStudyTeachChannel(tdNtStudyTeachInsert);
                map.put("channel", study_channel);

                map.put("recordKey", DynamicKey4Sample.getRecordKey(appID, study_channel, ts, r, uid, expiredTs));
                map.put("channelKey", DynamicKey4Sample.getChannelKey(appID, study_channel, ts, r, uid, expiredTs));
                map.put("ts", ts);
                map.put("r", r);
                map.put("uid", uid);
                map.put("appID", appID);
                map.put("expiredTs", expiredTs);
                map.put("type", "randomFreeKallTeacer");

                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setObj(map);

                /**
                 * 接通发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>) MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0; i < tdNtTeacherGetuisCached.size(); i++) {
                        if (!tdNtTeacherGetuisCached.get(i).getTea_id().equals(tdNtStudyTeachDetail.getTea_id())) {
                            if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                                AndroidTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());
                            } else {
                                IosTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());

                            }
                        }


                    }
                }
            } else if (tdNtStudyTeachDetail.getStudy_state().equals("6")) {
                Map<String, Object> result = new HashMap<>();
                result.put("type", params.getFunctionName());
                retInfo.setObj(result);
                retInfo.setMark("1");
                retInfo.setTip("老师拒绝");
                retInfo.setObj(result);
            } else {
                tdNtStudyTeachDetail.setStudy_state("6");
                tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtLiveDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                retInfo.setMark("1");
                retInfo.setTip("取消成功");
                Map<String, Object> result = new HashMap<>();
                result.put("type", "randomFreeKallTeacer");
                retInfo.setObj(result);
                /**
                 * 取消发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>) MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0; i < tdNtTeacherGetuisCached.size(); i++) {
                        if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                            AndroidTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());
                        } else {
                            IosTeacherGetui.Cancel("NativeTalk", "Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(), tdNtStudyTeachDetail.getStt_id());

                        }

                    }
                }
            }

        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo joinChat() {
        String logInfo = this.getClass().getName() + ":joinChat:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();

        Map<String, Object> map = new HashMap<>();
        map.put("type", "joinChat");
        retInfo.setMark("0");
        retInfo.setTip("成功");
        retInfo.setObj(map);

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo exitChat() {
        String logInfo = this.getClass().getName() + ":exitChat:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();

        Map<String, Object> map = new HashMap<>();
        map.put("type", "exitChat");
        retInfo.setMark("0");
        retInfo.setTip("成功");
        retInfo.setObj(map);

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo handsUp(Params params) {
        String logInfo = this.getClass().getName() + "handsUp:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> map1 = params.getParams();
            int live_cou_his_id = Integer.valueOf(map1.get("live_cou_his_id").toString());
            int count = tdNtLiveDao.findTdNtLiveMembWaitCount(live_cou_his_id);
            //判断学生排队的人数是否小于15人
            if (15 > count) {
                TdNtMember member = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
                Map<String, Object> mapParams = new HashMap<>();
                mapParams.put("live_cou_his_id", live_cou_his_id);
                mapParams.put("memb_id", member.getMemb_id());
                mapParams.put("live_memb_state", "2");//设置状态为举手
                tdNtLiveDao.updateTdNtLiveMemb(mapParams);

                Map<String, Object> map = new HashMap<>();
                map.put("type", "studentHandsUp");
                map.put("member", member);
                map.put("identifier", Constant.member + member.getMemb_id());
                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setObj(map);
            } else {
                Map<String, Object> map = new HashMap<>();
                map.put("type", "studentHandsUp");
                retInfo.setMark("-1");
                retInfo.setTip("排队人数不能超过15人");
                retInfo.setObj(map);
            }
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("系统异常");
            logger.error(e.getMessage(), e);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo handsDown(Params params) {
        String logInfo = this.getClass().getName() + "handsDown:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtMember member = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
            Map<String, Object> map1 = params.getParams();
            int live_cou_his_id = Integer.valueOf(map1.get("live_cou_his_id").toString());
            Map<String, Object> mapParams = new HashMap<>();
            mapParams.put("live_cou_his_id", live_cou_his_id);
            mapParams.put("memb_id", member.getMemb_id());
            mapParams.put("live_memb_state", "1");//设置状态为放弃举手(即进入直播状态)
            tdNtLiveDao.updateTdNtLiveMemb(mapParams);

            Map<String, Object> map = new HashMap<>();
            map.put("type", "studentHandsDown");
            map.put("member", member);
            map.put("identifier", Constant.member + member.getMemb_id());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("系统异常");
            logger.error(e.getMessage(), e);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo letMembTalk(Params params) {
        String logInfo = this.getClass().getName() + "membTalk:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> map1 = params.getParams();
            int live_cou_his_id = Integer.valueOf(map1.get("live_cou_his_id").toString());
            int memb_id = Integer.valueOf(map1.get("memb_id").toString());
            TdNtMember member = tdNtLiveDao.findTdNtMember(memb_id);
            int count = tdNtLiveDao.findTdNtLiveMembTalkCount(live_cou_his_id);
            if (2 > count) {
                Map<String, Object> mapParams = new HashMap<>();
                mapParams.put("live_cou_his_id", live_cou_his_id);
                mapParams.put("memb_id", memb_id);
                mapParams.put("live_memb_state", "3");//设置状态为上麦
                tdNtLiveDao.updateTdNtLiveMemb(mapParams);

                Map<String, Object> map = new HashMap<>();
                map.put("type", "letStudentTalk");
                map.put("member", member);
                map.put("identifier", Constant.member + memb_id);
                retInfo.setMark("0");
                retInfo.setTip("Succeed");
                retInfo.setObj(map);
            } else {
                Map<String, Object> map = new HashMap<>();
                map.put("type", "letStudentTalk");
                retInfo.setMark("-1");
                retInfo.setTip("You can Speak with only 2 students at the same time");
                retInfo.setObj(map);
            }

        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("System Error");
            logger.error(e.getMessage(), e);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo letMembStopTalk(Params params) {
        String logInfo = this.getClass().getName() + "membStopTalk:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> map1 = params.getParams();
            int live_cou_his_id = Integer.valueOf(map1.get("live_cou_his_id").toString());
            int memb_id = Integer.valueOf(map1.get("memb_id").toString());
            TdNtMember member = tdNtLiveDao.findTdNtMember(memb_id);
            Map<String, Object> mapParams = new HashMap<>();
            mapParams.put("live_cou_his_id", live_cou_his_id);
            mapParams.put("memb_id", memb_id);
            mapParams.put("live_memb_state", "1");//设置状态为下麦(即进入直播状态)
            tdNtLiveDao.updateTdNtLiveMemb(mapParams);

            Map<String, Object> map = new HashMap<>();
            map.put("type", "letStudentStopTalk");
            map.put("member", member);
            map.put("identifier", Constant.member + memb_id);
            retInfo.setMark("0");
            retInfo.setTip("Succeed");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("System Error");
            logger.error(e.getMessage(), e);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo membStopTalk(Params params) {
        String logInfo = this.getClass().getName() + "membStopTalk:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> map1 = params.getParams();
            int live_cou_his_id = Integer.valueOf(map1.get("live_cou_his_id").toString());
            TdNtMember member = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
            int memb_id = member.getMemb_id();
            Map<String, Object> mapParams = new HashMap<>();
            mapParams.put("live_cou_his_id", live_cou_his_id);
            mapParams.put("memb_id", memb_id);
            mapParams.put("live_memb_state", "1");//设置状态为下麦(即进入直播状态)
            tdNtLiveDao.updateTdNtLiveMemb(mapParams);

            Map<String, Object> map = new HashMap<>();
            map.put("type", "studentStopTalk");
            map.put("member", member);
            map.put("identifier", Constant.member + memb_id);
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
            logger.error(e.getMessage(), e);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public synchronized RetInfo openOrCloseCamera(Params params) {
        String logInfo = this.getClass().getName() + "openOrCloseCamera:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> map1 = params.getParams();
            //是否开启摄像头 1:开启 0:关闭
            String open_flag = map1.get("open_flag").toString();
            int live_cou_his_id = Integer.valueOf(map1.get("live_cou_his_id").toString());
            TdNtMember member = (TdNtMember) MemcachedUtils.get(params.getMustParams().get("token"));
            int memb_id = member.getMemb_id();
            Map<String, Object> mapParams = new HashMap<>();
            mapParams.put("live_cou_his_id", live_cou_his_id);
            mapParams.put("memb_id", memb_id);
            if ("1".equals(open_flag)) {
                mapParams.put("live_memb_state", "4");//设置状态为上麦(开启摄像头)
            } else {
                mapParams.put("live_memb_state", "3");//设置状态为上麦(关闭摄像头)
            }
            tdNtLiveDao.updateTdNtLiveMemb(mapParams);

            Map<String, Object> map = new HashMap<>();
            map.put("type", "studentOpenOrCloseCamera");
            map.put("open_flag", open_flag);
            map.put("member", member);
            map.put("identifier", Constant.member + memb_id);
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
            logger.error(e.getMessage(), e);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public TdNtMember findTdMembByReserveId(int reserve_id) {
        return tdNtLiveDao.findTdMembByReserveId(reserve_id);
    }

    public void setTdNtLiveDao(TdNtLiveDao tdNtLiveDao) {
        this.tdNtLiveDao = tdNtLiveDao;
    }
}
